package com.helloae.chap05;

import java.io.File;

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geodatabase.Field;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 读取shp文件，给shp文件增加/删除一个字段。<br>
 * 示例章节：5.4.1
 * 
 * @author zhushijie
 * 
 */
public class AddDeleteField {

	public static void main(String[] args) throws Exception {
		// 初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
		
		//使用ShapefileWorkspaceFactory.openFromFile的方法创建一个Workspace
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
						+ "DataSet" + File.separator + "chap05", 0);
		//加载图层
		IFeatureClass pFeatureClass = pFeatureWorkspace
				.openFeatureClass("idcounty");
		IFields pFields = pFeatureClass.getFields();
		System.out.println("本图层中共有" + pFields.getFieldCount() + "个字段。");
		
		
		Field pField1 = new Field();
		pField1.setName("Pop1990Pop");
		pField1.setType(esriFieldType.esriFieldTypeInteger);
		pField1.setLength(8);
		
		//这里较为奇怪，delete后pFields.getFieldCount()不会变小
		//但是add后pFields.getFieldCount()会变大
		//不过文件确实发生变化，暂时没找到原因
		pFeatureClass.addField(pField1);
		System.out.println("增加字段成功！");
		pFeatureClass.deleteField(pField1);
		System.out.println("删除字段成功！");
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
